//
//  StoreSandBox.m
//  课堂代码重构
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "StoreSandBox.h"
@import UIKit;

@interface StoreSandBox ()

@property (nonatomic, retain) NSCache *memCache;


@end


@implementation StoreSandBox

-(void)dealloc {
    
    [_memCache release];
    [super dealloc];
    
}

#pragma mark - 单例

+ (instancetype)shareStoreSandBox {
    
    static StoreSandBox *store = nil;
    
    if (store == nil) {
        store = [[StoreSandBox alloc] init];
    }
    
    return store;
    
    
}

- (NSCache *)memCache {
    
    if (_memCache == nil) {
        _memCache = [[NSCache alloc] init];
    }
    
    return [[_memCache retain] autorelease];
}






#pragma mark - getAddress
/** 沙盒地址*/
- (NSString *) pathOfSandBox {
    
    
    return NSHomeDirectory();
    
    
}

/** Documents 文件夹地址 */
- (NSString *) pathOfDocuments {
    
   return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    
}

/** Library 文件夹地址 */
- (NSString *) pathOfLibrary {
    
    
    return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    
    
}

/** Caches 文件夹地址*/
- (NSString *) pathOfCaches {
    
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    
}

/** tmp 文件夹地址*/
- (NSString *) pathOfTmp {
    
    return NSTemporaryDirectory();
    
}

/** APP地址 捆绑包地址*/
- (NSString *) pathForApp {
    
    return [[NSBundle mainBundle] bundlePath];
    
}


#pragma mark - storeFile
/** 存字符串*/
- (BOOL) storeWithString:(NSString *)content
                  forKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
}

#warning changeMethod
/** 存数组*/
- (BOOL) storeWithArray:(NSArray *)arrar
                 forKey:(NSString *)key {
    NSString *path = [[self pathOfCaches] stringByAppendingPathComponent:key];
    return [arrar writeToFile:path atomically:YES];
//    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
//    if (arr == nil) {
//        arr = [NSMutableArray array];
//    }
//    [arr removeAllObjects];
//    [arr addObjectsFromArray:arrar];
//    return [NSKeyedArchiver archiveRootObject:arrar toFile:path];
    
    
    
}

/** 存字典*/
- (BOOL) storeWithDictionary:(NSDictionary *)dictionary
                      forKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [dictionary writeToFile:path atomically:YES];
    
    
}
/** 存可变字典*/
- (BOOL) storeWithMutableDictionary:(NSMutableDictionary *)mDictionary
                             forKey:(NSString *)key {
    
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    return [mDictionary writeToFile:path atomically:YES];

}


/** 存图像*/
- (BOOL) storeWithImageName:(NSString *)name
                     forKey:(NSString *)key {
    
    NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:name], 1);
    if (!data) {
        data = UIImagePNGRepresentation([UIImage imageNamed:name]);
        
    }
   
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [data writeToFile:path atomically:YES];
    
    
    
    
}

/** 存Data文件*/
- (BOOL) storeFileWithData:(NSData *)data
                    forKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [data writeToFile:path atomically:YES];
}

#pragma mark - 自定义
- (void)storeByArchiver:(id)object key:(NSString *)key {
    NSString *path = [[self pathOfCaches] stringByAppendingPathComponent:key];
    [NSKeyedArchiver archiveRootObject:object toFile:path];
}
- (NSMutableArray *)readByArchiver:(NSString *)key {
    NSString *path = [[self pathOfCaches] stringByAppendingPathComponent:key];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}
/** 非foundation内的框架类 写入*/
- (BOOL) StoreByArchiverWithObject:(id)object
                            forKey:(NSString *)key {
    
    NSMutableData *data = [NSMutableData data];
    
    // 归档过程
    
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    [archiver encodeObject:object forKey:key];
    
    [archiver finishEncoding];
    
    [archiver release];
    
    [self StoreMemCacheWithObject:object forKey:key];
    
    
    return [self storeFileWithData:data forKey:key];
}

/** 存入虚拟缓存*/
- (void) StoreMemCacheWithObject:(id)object
                          forKey:(NSString *)key {
    
    [self.memCache setObject:object forKey:key];
}

#pragma mark - readFile

/** 取字符串*/
- (NSString *) readWithStringForKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
}

/** 取数组*/
- (NSArray *) readWithArrayForKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    
    return [NSArray arrayWithContentsOfFile:path];
//    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
//    return arr;
}

/** 取字典*/
- (NSDictionary *) readWithDictionaryForKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [NSDictionary dictionaryWithContentsOfFile:path];
    
    
}

/** 取可变字典*/
- (NSMutableDictionary *) readWithMutableDictionaryForKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    return [NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    
}


/** 取图像*/
- (UIImage *) readWithImageForKey:(NSString *)key {
    
    NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    
    return [UIImage imageWithData:data];
    
}

/** 非foundation内的框架类 读取*/
- (id) readByUnArchiverWithObjectForKey:(NSString *)key {
    
    NSData *data = [self readMemCacheWithObjectForKey:key];
    
    if (data == nil) {
        
        NSString *path = [[self pathOfDocuments] stringByAppendingPathComponent:key];
       data = [NSData dataWithContentsOfFile:path];

    }
    
    
    // 反归档
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    id result = [unArchiver decodeObjectForKey:key];
    [unArchiver finishDecoding];
    
    [unArchiver release];
    
    return result;
    
}

/** 从虚拟内存中读取*/
- (id) readMemCacheWithObjectForKey:(NSString *)key {
    
    return [self.memCache objectForKey:key];
}

#pragma mark - 在缓存区 管理文件夹


/** 创建文件管理器*/

- (NSFileManager *) createManager {
    
   return [NSFileManager defaultManager];
    
    
}

/** 创建某区文件夹*/
- (BOOL) createDirectoryAtPath:(NSString *)path
                       forName:(NSString *)name {
    
    
    NSString *address = [path stringByAppendingPathComponent:name];
    
    
    // 在指定路径中创建给定属性的目录。
   return [[self createManager] createDirectoryAtPath:address withIntermediateDirectories:YES attributes:nil error:nil];
  
}

/** 删除某区文件夹*/
- (BOOL) removeDirectoryAtCachesOfFileName:(NSString *)name {
    
    NSString *path = [[self pathOfCaches] stringByAppendingPathComponent:name];
    
    return [[self createManager] removeItemAtPath:path error:nil];
    
}

- (float)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    long long size;
    if ([fileManager fileExistsAtPath:path]) {
        /** 获取当前缓存路径下所有子路经. */
        NSArray *childFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            /** 计算文件大小. */
        long long    fileSize = [[fileManager attributesOfItemAtPath:absolutePath error:nil]fileSize ];
            size += fileSize;
        }
        folderSize = size/ (1000*1000);
    
    }
    return folderSize;
}




@end
